﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Application.Admin.Tour
{
    public class OpenTourService : IOpenTourService
    {
        ITourClassService claService;

        ITourPicService tourPicService;

        ITourService tourService;

        ITourSightService tourSightService;

        Application.Admin.Sight.ISightPointService sightService;

        Application.Admin.Sight.IOpenSightService openSightService;

        public OpenTourService(ITourClassService cla,
            ITourPicService tourPic,
            ITourService tour,
            ITourSightService tourSight,
            Application.Admin.Sight.ISightPointService sight,
            Application.Admin.Sight.IOpenSightService openSight)
        {
            if (cla == null)
            {
                throw new ArgumentNullException("claService is null");
            }
            if (tourPic == null)
            {
                throw new ArgumentNullException("tourPicService is null");
            }
            if (tour == null)
            {
                throw new ArgumentNullException("tourService is null");
            }
            if (tourSight == null)
            {
                throw new ArgumentNullException("tourSightService is null");
            }

            if (sight == null)
            {
                throw new ArgumentNullException("sightService is null");
            }
            if (openSight == null)
            {
                throw new ArgumentNullException("openSightService is null");
            }
            claService = cla;
            tourPicService = tourPic;
            tourService = tour;
            tourSightService = tourSight;
            sightService = sight;
            openSightService = openSight;
        }

        public List<Application.Admin.Dto.OpenTourDefaultDto> GetAllTour()
        {
            List<Application.Admin.Dto.OpenTourDefaultDto> all = null;
            var entity = tourService.GetList().Where(e => e.State == true);
            if (entity.Any())
            {
                all = new List<Dto.OpenTourDefaultDto>();
                Application.Admin.Dto.OpenTourDefaultDto temp = null;
                foreach (var item in entity)
                {
                    temp = new Dto.OpenTourDefaultDto();
                    temp.Id = item.Id;
                    temp.Name = item.Name;
                    var span = System.DateTime.Parse("2012-05-01");
                    var diff = item.ModifyTime - span;
                    temp.Version = (long)diff.TotalMinutes;
                    all.Add(temp);
                }
            }
            return all;
        }

        public Application.Admin.Dto.OpenTourDetailDto GetTourDetailByTourId(int id)
        {
            var tour = tourService.Get(id);
            Application.Admin.Dto.OpenTourDetailDto de = null;
            if (tour != null)
            {
                de = new Application.Admin.Dto.OpenTourDetailDto();
                de.CustomDateTime = tour.CustomDateTime;
                de.Id = tour.Id;
                de.Name = tour.Name;
                de.Remark = tour.Remark;
                var span = System.DateTime.Parse("2012-05-01");
                var diff = tour.ModifyTime - span;
                de.Version = (long)diff.TotalMinutes;
                de.TourSight = new List<Dto.OpenTourSightDto>();
                Application.Admin.Dto.OpenTourSightDto temp = null;
                var tourSight = tourSightService.GetList().Where(e => e.TourId == tour.Id);
                foreach (var item in tourSight)
                {
                    temp = new Dto.OpenTourSightDto();
                    temp.NextTips = string.IsNullOrEmpty(item.NextTips) ? "" : item.NextTips.Trim();
                    temp.Remark = string.IsNullOrEmpty(item.Remark) ? "" : item.Remark.Trim();
                    temp.SightId = item.SightId;
                    temp.CustomDateTime = string.IsNullOrEmpty(item.CustomDateTime) ? "" : item.CustomDateTime.Trim();
                    temp.Day = item.Day;
                    temp.IsShowInMap = item.IsShowInMap ? 1 : 0;
                    var sight = sightService.Get(item.SightId);
                    if (sight != null)
                    {
                        temp.SightLat = sight.Lat;
                        temp.SightLon = sight.Lon;
                        temp.SightName = string.IsNullOrEmpty(sight.Name) ? "" : sight.Name.Trim();
                        temp.SightAreaId = sight.AreaId;
                        var sightOpenPic = openSightService.GetSightLatestOpenPic(sight);
                        if (sightOpenPic != null)
                        {
                            temp.SightPic = sightOpenPic.Name;
                        }
                        else
                        {
                            temp.SightPic = "";
                        }
                    }
                    else
                    {
                        temp.SightLat = 0.0;
                        temp.SightLon = 0.0;
                        temp.SightName = "";
                        temp.SightPic = "";
                    }
                    temp.SortNum = item.SortNum;
                    de.TourSight.Add(temp);
                }
                de.TourSight = de.TourSight.OrderBy(e => e.SortNum).ToList();
            }
            return de;
        }
    }
}
